//
//  RCMerchantDetailTrafficViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-10.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantDetailTrafficViewController.h"
#import "RCMerchantDetailTrafficDataModel.h"
#import "UIColor-Expanded.h"
#import "NSArray+EX.h"
#import "RCMerchantDetailTrafficCell.h"
#import "CommonDefines.h"
#import "FrameworkRelativeDefines.h"
#import <MessageUI/MessageUI.h>
#import "AppDelegate.h"
#import "NSString+NSString_Ex_.h"

#define kTableViewFrame            CGRectMake(0,0,320,UIScreenHeight-64)

@interface RCMerchantDetailTrafficViewController ()<RCMerchantTrafficDelegate,MFMessageComposeViewControllerDelegate>

@property(nonatomic,retain) UITableView *trafficTabelView;
@property(nonatomic,assign) int64_t requestID;
@property(nonatomic,retain) MBProgressHUDEx *progressHub;
@property(nonatomic,retain) RCMerchantDetailTrafficDataModel *trafficDataModel;
@property(nonatomic,retain) NSString *shopID;
@property(nonatomic,retain) NSString *shopName;

@end

@implementation RCMerchantDetailTrafficViewController
@synthesize trafficTabelView = _trafficTabelView;
@synthesize requestID = _requestID;
@synthesize progressHub = _progressHub;
@synthesize trafficDataModel = _trafficDataModel;
@synthesize shopID = _shopID;
@synthesize shopName = _shopName;

-(id)initWithShopID:(NSString *)shopid withShopName:(NSString *)shopName{
    self = [super init];
    if (self) {
        self.shopID = shopid;
        self.shopName = shopName;
    }
    return self;
}

-(void)loadView{
    [super loadView];
    self.navigationItem.titleView = [self createTwolineTitleView];
    
}

-(UIView *)createTwolineTitleView{
    UIView *titleView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 44)]autorelease];
    UILabel *upperLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 150, 15)];
    upperLabel.backgroundColor = [UIColor clearColor];
    upperLabel.textColor = [UIColor whiteColor];
    upperLabel.textAlignment = UITextAlignmentCenter;
    upperLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    upperLabel.shadowColor = [UIColor colorWithHex:0xff2c2c2];
    upperLabel.shadowOffset = CGSizeMake(0, 0.5);
    upperLabel.text = @"商户详情信息";
    [titleView addSubview:upperLabel];
    [upperLabel release];
    
    UILabel *downLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 24, 150, 15)];
    downLabel.backgroundColor = [UIColor clearColor];
    downLabel.textColor = [UIColor whiteColor];
    downLabel.textAlignment = UITextAlignmentCenter;
    downLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    downLabel.shadowColor = [UIColor colorWithHex:0xff2c2c2];
    downLabel.shadowOffset = CGSizeMake(0, 0.5);
    downLabel.text = self.shopName;
    [titleView addSubview:downLabel];
    [downLabel release];
    return titleView;
}

-(void)dealloc{
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
    _requestID = kInvalidRequestId;
    self.trafficTabelView = nil;
    self.trafficDataModel = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    if (!_trafficTabelView) {
        _trafficTabelView = [[UITableView alloc]initWithFrame:kTableViewFrame style:UITableViewStylePlain];
        _trafficTabelView.backgroundColor = [UIColor clearColor];
        _trafficTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _trafficTabelView.delegate = self;
        _trafficTabelView.dataSource = self;
    }
    [self.viewContainer addSubview:_trafficTabelView];
    
    [self requestData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
     _requestID = kInvalidRequestId;
}

-(void)requestData{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance]requestMerchantDetailTraffic:self.shopID withDelegte:self];
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        self.trafficDataModel = (RCMerchantDetailTrafficDataModel *)dataModel;
        [_trafficTabelView reloadData];
    }
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (nil == self.trafficDataModel || nil == self.trafficDataModel.itemListArray || [self.trafficDataModel.itemListArray count] == 0) {
        return 0;
    }
    return [self.trafficDataModel.itemListArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (nil == self.trafficDataModel || nil == self.trafficDataModel.itemListArray || [self.trafficDataModel.itemListArray count] == 0) {
        return 0;
    }
    RCMerchantDetailTrafficItem *detailInfo = [self.trafficDataModel.itemListArray safeObjectAtIndex:[indexPath row]];
    if ([NSString stringIsNullOrEmpty:detailInfo.content]) {
        return 0.0f;
    }
    BOOL isFirst = NO;
    if ([indexPath row] == 0) {
        isFirst = YES;
    }
    return [RCMerchantDetailTrafficCell getCellHeight:detailInfo isFirstCell:isFirst];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"MerchatnDetailTrafficTabelViewCell";
    RCMerchantDetailTrafficCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (nil == cell) {
        cell = [[[RCMerchantDetailTrafficCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
        cell.delegate = self;
    }
    NSInteger row = [indexPath row];
    RCMerchantDetailTrafficItem *detailInfo = [self.trafficDataModel.itemListArray safeObjectAtIndex:row];
    BOOL isFirst = NO;
    if ([indexPath row] == 0) {
        isFirst = YES;
    }
    [cell setContentWithDataModel:detailInfo isFirstCell:isFirst];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(void)mainButtonClicked:(NSString *)content{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        if ([messageClass canSendText]) {
            [self displaySMSComposerSheet:content];
        }else{
            [RCAppDelegate alertWithCustomizeString:@"您的设备不支持短信功能"];
        }
    }
}

-(void)displaySMSComposerSheet:(NSString *)content{
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate = self;
    picker.body = content;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    switch (result) {
        case MessageComposeResultCancelled:
            
            break;
        case MessageComposeResultFailed:
            [RCAppDelegate alertWithCustomizeString:@"发送失败"];
            break;
        case MessageComposeResultSent:
            [RCAppDelegate alertWithCustomizeString:@"发送成功"];
            break;
        default:
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
